import itsdangerous
import redis
from django.shortcuts import render
from rest_framework.generics import RetrieveAPIView

from app1.models import *
from app1.ser import *
from rest_framework.views import APIView
from rest_framework.response import Response
import jwt

from djangoProject6.settings import SECRET_KEY


class Login(APIView):
    def post(self,request):
        username=request.POST.get('username')
        a=Yonghu.objects.filter(username=username).first()
        if a:
            token=jwt.encode({'uid':a.id},'123')
            return Response({'msg':'登陆成功','code':200,'token':token})
        else:
            return Response({'msg':'登陆失败','code':400})

    def get(self,request):
        a=Libary.objects.all()
        b=LibarySer(a,many=True)
        return Response(b.data)

class Cate1(APIView):
    def get(self,request):
        a=Libary.objects.all()
        b=LibarySer(a,many=True)
        return Response(b.data)

class Add(APIView):
    def post(self,request):
        pid=request.data.get('pid')
        request.data['uid']=request.uid
        if Che.objects.filter(pid_id=pid,uid_id=request.uid).first():
            return Response({'msg':'已加入','code':200})
        else:
            num=CheSer(data=request.data)
            num.is_valid(raise_exception=True)
            num.save()
            return Response({'msg':'加入成功'})

    def get(self,request):
        id=request.GET.get('id')
        a=Libary.objects.filter(id=id)
        b=LibarySer(a,many=True)
        return Response(b.data)

import datetime
from django.db import transaction

class Chi(APIView):
    def put(self,request):
        num=request.data.get('num')
        cart=request.data.get('cart')
        Che.objects.filter(id=cart).update(num=num)
        return Response({'msg':66})
    def get(self,request):
        a=Che.objects.filter(uid_id=request.uid)
        b=CheSer1(a,many=True)
        return Response(b.data)
    def delete(self,request):
        id=request.GET.get('id')
        Che.objects.filter(id=id).delete()
        return Response({'msg':'删除成功'})
#
# def redis_connection():
#     return redis.Redis(host='127.0.0.1', port=6379, password='')
# from rest_framework.views import APIView
# salt=SECRET_KEY
# t=itsdangerous.TimedJSONWebSignatureSerializer(salt,expires_in=600)
# r = redis_connection()
#
# class Che(RetrieveAPIView):##购物车
#     # queryset = GoodModel.objects.all()
#     serializer_class = CheSer
#
#     def get(self, request, pk, *args, **kwargs):
#         token = request.query_params.get('token')
#         print(token)
#         try:
#             data = t.load(token)
#             uid_id = data.get('id')
#         except Exception as e:
#             print(e)
#             return Response({'code': 404})
#         else:
#             uid_key = 'uid_history_%s' % uid_id
#             uid_history_list = r.lrange(uid_key, 0, -1)
#             if not uid_history_list:
#                 r.lpush(uid_key, pk)
#             else:
#                 r.lrem(uid_key, 1, pk)
#                 r.lpush(uid_key, pk)
#             r.ltrim(uid_key, 0, 2)
#         return self.retrieve(request, *args, **kwargs)